package cn.bxshey.resourceSharing.utils;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Random;

/**
 *发送邮件
 * 交由spring容器管理以后，才能进行使用，否则可能出现无法注入JavaMailSender邮件类的问题
 */
@Configuration
@Data
public class MyMail {
    @Value("${emailConfig.from}")
    private String from;

    @Autowired
    private JavaMailSender javaMailSender;
    public void sendSimpleMail(String subject,String to,String text,boolean html) throws MessagingException {
        //一个复杂的邮件`
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //进行组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, html);
        helper.setSubject(subject); // 发送的主题
        helper.setText(text, html);//发送的文本内容  以及是否支持html格式
        helper.setTo(to); //收件人
        helper.setFrom(from); //发送人
        javaMailSender.send(mimeMessage);
    }



    /**
     *
     *  [subject:标题, text:内容, html,Boolean html:是否进行html解析
     * email:接收人的邮件地址, attachmentMap:附件的名称和文件路径]
     *  void
     *  你自己
     *  2020/2/27
     */
//    public void sendMimeEmail(String subject, String text, Boolean html,
//                              String email, Map<String, String> attachmentMap) throws MessagingException {
//        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
//        //一个复杂的邮件`
//        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//        //进行组装
//        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, html);
//        helper.setSubject(subject);
//        helper.setText(text, html);
//
//        //附件
//        if (attachmentMap != null) {
//            //map.entrySet()得到的是set集合，可以使用迭代器遍历
//            for (Map.Entry<String, String> entry : attachmentMap.entrySet()) {
//                helper.addAttachment(entry.getKey(),
//                        //附件名称,要写好文件的后缀,不要少写和写错
//                        new File(entry.getValue()));
//                //附件的文件地址,可以写绝对路径,若是相对路径,如./1.png,代表的是resources下的1.png
//            }
//        }
//        helper.setTo(email); //发送给谁
//        helper.setFrom(Objects.requireNonNull(javaMailSender.getUsername())); //谁发送的
//        javaMailSender.send(mimeMessage);
//    }


    //随机生成六位验证码
    public String myMailCaptcha() {
        Random rnd = new Random();
        StringBuilder sb = new StringBuilder();
        int mark = rnd.nextInt(5)+1;
        for (int i = 0; i < 6; i++) {
            if(mark-1 == i || mark % (i+1) == 0){
                int index = rnd.nextInt(25)+65;
                sb.append((char) index);
            }else{
                int index = rnd.nextInt(9);
                sb.append(index);
            }
        }
        return sb.toString();
    }

}
